import requests
import json
import time
import hmac
import hashlib
import base64
from datetime import datetime

# 请求地址
url = "http://www.okx.com/api/v5/waas/coin/price-list"

# 请求头
headers = {
    "Content-Type": "application/json",
    "OK-ACCESS-PROJECT": "89650fc9e2a79da717c5954cb7607e2b",
    "OK-ACCESS-KEY": "b4be682d-825c-4bb8-be01-112e1974cf42",
    "OK-ACCESS-PASSPHRASE": "DAJIBAfafafa888..."
}

# 生成时间戳
timestamp = (
                   str(time.time()).split(".")[0]
                   + "."
                   + str(time.time()).split(".")[1][:3]
          )

# 构造消息
message = str(timestamp) + 'POST' + '/api/v5/waas/coin/price-list' + str(json.dumps({
    "tokenList": [
        {
            "chainId": 1,
            "tokenAddressList": ["0xdac17f958d2ee523a2206206994597c13d831ec7"]
        },
        {
            "chainId": 56
        }
    ]
}))

# 计算签名
secret_key = '3F0E37A68EDC1089D56E175D06533CD4'
signature = base64.b64encode(hmac.new(bytes(secret_key, encoding="utf8"), bytes(message, encoding="utf-8"), digestmod="sha256").digest())
     
# 添加必要的请求头信息
headers['OK-ACCESS-TIMESTAMP'] = timestamp
headers['OK-ACCESS-SIGN'] = signature 

# 发送请求
print('url'+url)
print('headers'+str(headers))
print('data'+json.dumps({
    "tokenList": [
        {
            "chainId": 1,
            "tokenAddressList": ["0xdac17f958d2ee523a2206206994597c13d831ec7"]
        },
        {
            "chainId": 56
        }
    ]
}))
response = requests.post(url, headers=headers, data=json.dumps({
    "tokenList": [
        {
            "chainId": 1,
            "tokenAddressList": ["0xdac17f958d2ee523a2206206994597c13d831ec7"]
        },
        {
            "chainId": 56
        }
    ]
}), params=None)

# 打印响应结果
print(response.text())
####  
    #    if result.get("code") and result.get("code") != "0":
    #        return None, result
    #    return result, None